using UnityEngine;
using System.Collections;

public class PlayerCharacterBehaviour : CharacterBehaviour
{
	//public GUIText g;
	private int maxAction = 1, currentAction = 0;
	
	[SerializeField]
	private ManagerAnimationUV effectEnergyUp = null, effectEnergyDown = null;
	
	//[HideInInspector]
	public bool onGroundUp = false, onGroundDown = false;
	
	private ManagerSurvivalMode survival;
	
	//private Follower cameraFollower;
	protected override void Awake()
	{
		base.Awake();
		
		survival = (ManagerSurvivalMode)FindObjectOfType(typeof(ManagerSurvivalMode));
		//cameraFollower = (Follower)FindObjectOfType(typeof(Follower));
	}
	
	protected override void Update () 
	{
		if(!onHit && hp > 0 && ManagerGame.enableAction)
		{
			UpdateInput();
		}
		
		if(onGroundUp && base.OnGround)
		{
			if(!effectEnergyUp.gameObject.active)
			{ effectEnergyUp.gameObject.active = true; }
			base.hp = Mathf.Min(100, hp + 5f * Time.deltaTime);
			onGroundUp = false;
		}
		else
		{
			if(effectEnergyUp.gameObject.active)
			{ effectEnergyUp.gameObject.active = false; }
		}
		
		if(onGroundDown && base.OnGround)
		{
			if(!effectEnergyDown.gameObject.active)
			{ effectEnergyDown.gameObject.active = true; }
			base.hp = Mathf.Max(0, hp - 5f * Time.deltaTime);
			onGroundDown = false;
		}
		else
		{
			if(effectEnergyDown.gameObject.active)
			{ effectEnergyDown.gameObject.active = false; }
		}
		
		base.Update();
	}
	
	private void UpdateInput()
	{
		if(!base.specialAir && !base.specialShot)
		{
			if(base.OnGround)
			{
				currentAction = 0;
			}
			else
			{
				if(Input.GetKeyDown(KeyCode.DownArrow))
				{
					TryDash(new Vector2(0, -1));
				}
			}
			
			if(Input.GetKey(KeyCode.RightArrow))
			{
				//cameraFollower.NewOffset(new Vector3(100, 0, -10));
				base.TryMove(true);
			}
			else if(Input.GetKey(KeyCode.LeftArrow))
			{
				//cameraFollower.NewOffset(new Vector3(-100, 0, -10));
				base.TryMove(false);
			}
			
			if(Input.GetKeyDown(KeyCode.UpArrow))
			{
				if(currentAction < maxAction)
				{
					base.TryJump();
					
					if(base.OnLastJump)
					{
						currentAction = Mathf.Min(maxAction, currentAction + 1);
					}
				}
			}
			
			if(Input.GetKey(KeyCode.Z) && Input.GetKey(KeyCode.X) && hp > 75)
			{
				if(base.OnGround)
				{
					base.SpecialOnGround();
				}
				else
				{
					base.SpecialOnAir();
				}
			}
			else
			{
				if(Input.GetKeyDown(KeyCode.Z))
				{
					base.Shot(false);
				}
				else if(Input.GetKey(KeyCode.Z))
				{
					base.Shot(true);
				}
				
				if(Input.GetKey(KeyCode.X))
				{
					base.Melee();
				}
				
				if(Input.GetKeyDown(KeyCode.C))
				{
					TryDash(new Vector2(base._transform.localScale.x, 0));
				}
			}
		}
		
		if(base.hp > 0)
		{
			base.hp = Mathf.Max(0, hp - 1.66f * Time.deltaTime);
		}
		
		if(base.hitAnEnemy)
		{
			currentAction = Mathf.Max(0, currentAction - 1);
			base.hitAnEnemy = false;
		}
		
		if(base.diedAnEnemy != 0)
		{
			survival.MoreOneElimined(diedAnEnemy);
			hp = Mathf.Min(100, hp + (base.diedAnEnemy * 5));
			base.diedAnEnemy = 0;
		}
		
		//g.text = "ENERGIA : " + hp.ToString();
	}
	
	private void TryDash(Vector2 direction)
	{
		if(!base.OnDash && currentAction < maxAction)
		{
			currentAction = Mathf.Min(maxAction, currentAction + 1);
			base.Dash(direction);
		}
	}
	
	public bool TryMoveTo(Vector3 position)
	{
		if(Mathf.Abs(_transform.position.x - position.x) < 30)
			return true;
		else
		{
			if(_transform.position.x < position.x)
			{
				TryMove(true);
			}
			else
			{
				TryMove(false);
			}
		}
		
		return false;
	}
}
